
#include "session.h"

void session::start()
{
    while (1)
    {
        do_read();
    }
}

void session::do_read()
{
    auto self(shared_from_this());
    uint32_t *bufferLenPackage = new uint32_t[4];

    uint32_t lenPackage = socket_.read_some(boost::asio::buffer(bufferLenPackage, 4)); // get 4bypte of date size;
    cout << "..prepase get bufferLenPackage:" << bufferLenPackage[0] << endl;
    uint8_t *data_ = do_getdata(bufferLenPackage[0]);

    bool rtParseFromArray = imageBuff.ParseFromArray(data_, bufferLenPackage[0]);
    cout << "rtParseFromArray:" << rtParseFromArray << endl;
    int srcHigh = imageBuff.high();
    int srcWidth = imageBuff.width();
    int srcChannel = imageBuff.channel();
    int srcLen = srcHigh * srcWidth * srcChannel;

    // cout << "srcHigh=" << srcHigh << endl;
    // cout << "srcWidth=" << srcWidth << endl;
    // cout << "srcChannel" << srcChannel << endl;
    string strReciveIMage = imageBuff.dataimage();
    const char *tPtrBuff = strReciveIMage.c_str();
    uint8_t *PtrBuff = new uint8_t[strReciveIMage.size()];

    for (int i = 0; i < strReciveIMage.length(); i++)
    {
        PtrBuff[i] = tPtrBuff[i];
    }

#if 0
    cv::Mat tImgOrig(srcHigh, srcWidth, CV_8UC3, cv::Scalar(0, 0, 0));
    ucharToMat(PtrBuff, tImgOrig, 0); //

    std::string strTime = boost::posix_time::to_iso_string(
        boost::posix_time::second_clock::local_time());

    int pos = strTime.find('T');
    strTime.replace(pos, 1, std::string("-"));
    strTime.replace(pos + 3, 0, std::string(":"));
    strTime.replace(pos + 6, 0, std::string(":"));

    char m_cBuf[10 * 500];
    sprintf(m_cBuf, "./Receive_%s.jpg", strTime.c_str());
    std::cout << "m_cBuf" << m_cBuf << std::endl;
    cv::imwrite(m_cBuf, tImgOrig);
#endif
 
    
    bufferLenPackage = nullptr;
}

uint8_t *session::do_getdata(std::size_t length)
{

    auto self(shared_from_this());
    uint32_t totalReadeLen = 0;
    std::ofstream outfile_debug;

    uint8_t *TotalGetBuff = new uint8_t[length];

    uint8_t *ptrBuff = TotalGetBuff;

    while (true)
    {
        boost::system::error_code error;

        uint8_t *tGetBuff = new uint8_t[length];
        memset(tGetBuff, 0x00, length);

        uint32_t readeSomeLen = socket_.read_some(boost::asio::buffer(tGetBuff, length), error);

        cout << "readeSomeLen:" << readeSomeLen << endl;

        //  printf("tGetBuff=%d\n",tGetBuff);getchar();

        if (error == boost::asio::error::eof)
        {
            cout << "error == boost::asio::error::eof:  " << error.message() << endl;
            // break;
        }
        // Connection closed cleanly by peer.1D
        else if (error)
        {
            cout << " throw erro:" << error.message() << endl;
            throw error;
        }
        // Some other err

        totalReadeLen += readeSomeLen;
        memcpy(ptrBuff, tGetBuff, readeSomeLen);
        delete tGetBuff;
        ptrBuff = ptrBuff + (readeSomeLen / sizeof(uint8_t));
        //     cout << "mark: totalReadeLen:" << totalReadeLen << " length:" << length << endl;
        if (totalReadeLen == length - 4)
        {
#if 1

            //  cout << "mark: totalReadeLen:" << totalReadeLen << " length:" << length << endl;
            ImageInfo imageBuff;
            bool rtParseFromArray = imageBuff.ParseFromArray(TotalGetBuff, length);
            // cout << "rtParseFromArray:" << rtParseFromArray << endl;
            int srcHigh = imageBuff.high();
            int srcWidth = imageBuff.width();
            int srcChannel = imageBuff.channel();
            int srcLen = srcHigh * srcWidth * srcChannel;

            // cout << "srcHigh=" << srcHigh << endl;
            // cout << "srcWidth=" << srcWidth << endl;
            // cout << "srcChannel" << srcChannel << endl;
            string strReciveIMage = imageBuff.dataimage();
            const char *tPtrBuff = strReciveIMage.c_str();
            uint8_t *PtrBuff = new uint8_t[strReciveIMage.size()];

            for (int i = 0; i < strReciveIMage.length(); i++)
            {
                PtrBuff[i] = tPtrBuff[i];
            }

            cv::Mat tImgOrig(srcHigh, srcWidth, CV_8UC3, cv::Scalar(0, 0, 0));
            ucharToMat(PtrBuff, tImgOrig, 0); //

            std::string strTime = boost::posix_time::to_iso_string(
                boost::posix_time::second_clock::local_time());

            int pos = strTime.find('T');
            strTime.replace(pos, 1, std::string("-"));
            strTime.replace(pos + 3, 0, std::string(":"));
            strTime.replace(pos + 6, 0, std::string(":"));

            char m_cBuf[10 * 500];
            static int markReceive=0;
            sprintf(m_cBuf, "./Receive_%s_%d.jpg", strTime.c_str(),markReceive);
            markReceive++;
            std::cout << "m_cBuf" << m_cBuf << std::endl;
            cv::imwrite(m_cBuf, tImgOrig);

#endif

            cout << "mark: returnptrBuff~" << endl;

            char ok[2]={'o','k'};
            boost::asio::const_buffers_1 request(ok, 2);
          socket_.send(request);
            return TotalGetBuff;
        }

    } //eof `while(true)`
    return TotalGetBuff;
}
